// 轮播图逻辑
document.addEventListener('DOMContentLoaded', function() {
    const bannerWrapper = document.querySelector('.banner-wrapper');
    const bannerItems = document.querySelectorAll('.banner-item');
    const dots = document.querySelectorAll('.dot');
    const prevBtn = document.querySelector('.banner-arrow-left');
    const nextBtn = document.querySelector('.banner-arrow-right');
    let currentIndex = 0;
    const totalSlides = bannerItems.length;


    // 切换轮播图
    function goToSlide(index) {
        if (index < 0) index = totalSlides - 1;
        if (index >= totalSlides) index = 0;
        currentIndex = index;
        bannerWrapper.style.transform = `translateX(-${currentIndex * 100}%)`;
        // 更新圆点状态
        dots.forEach((dot, i) => {
            dot.classList.toggle('active', i === currentIndex);
        });
    }


    // 圆点点击事件
    dots.forEach((dot, i) => {
        dot.addEventListener('click', () => {
            goToSlide(i);
        });
    });


    // 左右箭头点击事件
    prevBtn.addEventListener('click', () => {
        goToSlide(currentIndex - 1);
    });
    nextBtn.addEventListener('click', () => {
        goToSlide(currentIndex + 1);
    });


    // 自动轮播
    let autoPlay = setInterval(() => {
        goToSlide(currentIndex + 1);
    }, 5000);


    // 鼠标悬停暂停轮播
    document.querySelector('.banner').addEventListener('mouseenter', () => {
        clearInterval(autoPlay);
    });
    document.querySelector('.banner').addEventListener('mouseleave', () => {
        autoPlay = setInterval(() => {
            goToSlide(currentIndex + 1);
        }, 5000);
    });


    // 回到顶部按钮逻辑
    const backToTopBtn = document.querySelector('.back-to-top');
    window.addEventListener('scroll', () => {
        if (window.scrollY > 300) {
            backToTopBtn.classList.add('active');
        } else {
            backToTopBtn.classList.remove('active');
        }
    });
    backToTopBtn.addEventListener('click', () => {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
});